package com.aries.ui.widget.demo.manager;

import com.aries.ui.view.tab.CommonTabLayout;
import com.aries.ui.view.tab.SegmentTabLayout;
import com.aries.ui.view.tab.SlidingTabLayout;
import com.aries.ui.view.tab.listener.CustomTabEntity;
import com.aries.ui.view.tab.listener.OnTabSelectListener;
import com.aries.ui.widget.demo.base.BaseFraction;
import ohos.aafwk.ability.fraction.Fraction;
import ohos.aafwk.ability.fraction.FractionAbility;
import ohos.aafwk.ability.fraction.FractionManager;
import ohos.aafwk.content.Intent;
import ohos.agp.components.*;
import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @Author: AriesHoo on 2018/7/23 11:13
 * @E-Mail: AriesHoo@126.com
 * Function: FlycoTabLay+ViewPager 使用管理类
 * Description:
 */
public class TabLayoutManager {

    private static volatile TabLayoutManager instance;

    private TabLayoutManager() {
    }

    public static TabLayoutManager getInstance() {
        if (instance == null) {
            synchronized (TabLayoutManager.class) {
                if (instance == null) {
                    instance = new TabLayoutManager();
                }
            }
        }
        return instance;
    }

    /**
     * 设置滑动 Tab SlidingTabLayout
     *
     * @param fragment fragment
     * @param tabLayout tabLayout
     * @param viewPager viewPager
     * @param tittles tittles
     * @param fragments fragments
     */
    public void setSlidingTabData(Fraction fragment, SlidingTabLayout tabLayout, PageSlider viewPager, List<String> tittles, List<Fraction> fragments) {
        setSlidingTabData(fragment, tabLayout, viewPager, tittles, fragments, null);
    }

    /**
     * 设置滑动 Tab SlidingTabLayout
     *
     * @param activity Ability
     * @param tabLayout tabLayout
     * @param viewPager viewPager
     * @param tittles tittles
     * @param fragments fragments
     */
    public void setSlidingTabData(FractionAbility activity, SlidingTabLayout tabLayout, PageSlider viewPager, List<String> tittles, List<Fraction> fragments) {
        setSlidingTabData(activity, tabLayout, viewPager, tittles, fragments, null);
    }

    /**
     * Fragment 里SlidingTabLayout 快速设置
     *
     * @param fragment fragment
     * @param viewPager viewPager
     * @param tittles tittles
     * @param fragments fragments
     * @param tabLayout tabLayout
     * @param listener listener
     */
    public void setSlidingTabData(Fraction fragment, SlidingTabLayout tabLayout, PageSlider viewPager, List<String> tittles, List<Fraction> fragments, OnTabSelectListener listener) {
        setViewPager(fragment, tabLayout, viewPager, tittles, fragments, listener);
        tabLayout.setViewPager(viewPager);
    }

    /**
     * FragmentActivity里SlidingTabLayout 快速设置
     *
     * @param activity Ability
     * @param viewPager viewPager
     * @param tittles tittles
     * @param fragments fragments
     * @param tabLayout tabLayout
     * @param listener listener
     */
    public void setSlidingTabData(FractionAbility activity, SlidingTabLayout tabLayout, PageSlider viewPager, List<String> tittles, List<Fraction> fragments, OnTabSelectListener listener) {
        setViewPager(activity, tabLayout, viewPager, tittles, fragments, listener);
        tabLayout.setViewPager(viewPager);
    }

    public void setCommonTabData(Fraction fragment, final CommonTabLayout tabLayout, final PageSlider viewPager, ArrayList<CustomTabEntity> tabs, List<Fraction> fragments) {
        setCommonTabData(fragment, tabLayout, viewPager, tabs, fragments, null);
    }

    public void setCommonTabData(FractionAbility activity, final CommonTabLayout tabLayout, final PageSlider viewPager, ArrayList<CustomTabEntity> tabs, List<Fraction> fragments) {
        setCommonTabData(activity, tabLayout, viewPager, tabs, fragments, null);
    }

    /**
     * 快速设置 Fragment CommonTabLayout
     *
     * @param fragment fragment
     * @param viewPager viewPager
     * @param tabs tabs
     * @param fragments fragments
     * @param tabLayout tabLayout
     * @param listener listener
     */
    public void setCommonTabData(Fraction fragment, final CommonTabLayout tabLayout, final PageSlider viewPager, ArrayList<CustomTabEntity> tabs, List<Fraction> fragments, final OnTabSelectListener listener) {
        setViewPager(fragment, tabLayout, viewPager, null, fragments, listener);
        tabLayout.setTabData(tabs);
    }

    /**
     * 快速设置CommonTabLayout
     *
     * @param activity Ability
     * @param viewPager viewPager
     * @param tabs tabs
     * @param fragments fragments
     * @param tabLayout tabLayout
     * @param listener listener
     */
    public void setCommonTabData(FractionAbility activity, final CommonTabLayout tabLayout, final PageSlider viewPager, ArrayList<CustomTabEntity> tabs, List<Fraction> fragments, final OnTabSelectListener listener) {
        setViewPager(activity, tabLayout, viewPager, null, fragments, listener);
        tabLayout.setTabData(tabs);
    }

    public void setSegmentTabData(Fraction fragment, final SegmentTabLayout tabLayout, final PageSlider viewPager, String[] titles, List<Fraction> fragments) {
        setSegmentTabData(fragment, tabLayout, viewPager, titles, fragments, null);
    }

    public void setSegmentTabData(FractionAbility activity, final SegmentTabLayout tabLayout, final PageSlider viewPager, String[] titles, List<Fraction> fragments) {
        setSegmentTabData(activity, tabLayout, viewPager, titles, fragments, null);
    }

    /**
     * 快速设置Fragment 里SegmentTabLayout
     *
     * @param fragment fragment
     * @param tabLayout tabLayout
     * @param viewPager viewPager
     * @param titles titles
     * @param fragments fragments
     * @param listener listener
     */
    public void setSegmentTabData(Fraction fragment, final SegmentTabLayout tabLayout, final PageSlider viewPager, String[] titles, List<Fraction> fragments, final OnTabSelectListener listener) {
        setViewPager(fragment, tabLayout, viewPager, Arrays.asList(titles), fragments, listener);
        tabLayout.setTabData(titles);
    }

    /**
     * 快速设置 FragmentActivity 里SegmentTabLayout
     *
     * @param activity activity
     * @param tabLayout tabLayout
     * @param viewPager viewPager
     * @param titles 标签数组
     * @param fragments fragment 数组
     * @param listener tab切换监听回调
     */
    public void setSegmentTabData(FractionAbility activity, final SegmentTabLayout tabLayout, final PageSlider viewPager, String[] titles, List<Fraction> fragments, final OnTabSelectListener listener) {
        setViewPager(activity, tabLayout, viewPager, null, fragments, listener);
        tabLayout.setTabData(titles);
    }

    /**
     * viewPager配合使用
     *
     * @param activity FragmentActivity或Fragment
     * @param viewPager 装载 Fragment的容器
     * @param tittles 标签数组
     * @param fragments 加载Fragment数组
     * @param listener tab切换回调
     * @param tabLayout tab切换回调
     */
    private void setViewPager(Object activity, final Object tabLayout, final PageSlider viewPager, List<String> tittles, List<Fraction> fragments, final OnTabSelectListener listener) {

        if (activity == null) {
            return;
        }
        if (! (activity instanceof FractionAbility) && ! (activity instanceof Fraction)) {
            return;
        }
        if (! (tabLayout instanceof CommonTabLayout) && ! (tabLayout instanceof SlidingTabLayout) && ! (tabLayout instanceof SegmentTabLayout)) {
            return;
        }
        viewPager.setProvider(getFragmentAdapter(activity, tittles, fragments));
        viewPager.setPageCacheSize(fragments.size());
        viewPager.addPageChangedListener(new PageSlider.PageChangedListener() {
            @Override
            public void onPageSliding(int i, float v, int i1) {

            }

            @Override
            public void onPageSlideStateChanged(int i) {

            }

            @Override
            public void onPageChosen(int position) {
                if ((tabLayout instanceof CommonTabLayout)) {
                    ((CommonTabLayout) tabLayout).setCurrentTab(position);
                } else if ((tabLayout instanceof SegmentTabLayout)) {
                    ((SegmentTabLayout) tabLayout).setCurrentTab(position);
                }
                if (listener != null) {
                    listener.onTabSelect(position);
                }
            }
        });

        if ((tabLayout instanceof CommonTabLayout)) {
            ((CommonTabLayout) tabLayout).setOnTabSelectListener(new OnTabSelectListener() {
                @Override
                public void onTabSelect(int position) {
                    viewPager.setCurrentPage(position);
                }

                @Override
                public void onTabReselect(int position) {
                    if (listener != null) {
                        listener.onTabReselect(position);
                    }
                }
            });
        } else if ((tabLayout instanceof SegmentTabLayout)) {
            ((SegmentTabLayout) tabLayout).setOnTabSelectListener(new OnTabSelectListener() {
                @Override
                public void onTabSelect(int position) {
                    viewPager.setCurrentPage(position);
                }

                @Override
                public void onTabReselect(int position) {
                    if (listener != null) {
                        listener.onTabReselect(position);
                    }
                }
            });
        }
    }

    /**
     * 快速设置适配器
     *
     * @param activity Fragment或FragmentActivity
     * @param tittles 标签列表
     * @param fragments Fragment列表
     * @return FragmentStatePagerAdapter适配器
     */
    private PageSliderProvider getFragmentAdapter(Object activity, final List<String> tittles, @NotNull final List<Fraction> fragments) {
        FractionManager manager = null;
        if (activity instanceof FractionAbility) {
            manager = ((FractionAbility) activity).getFractionManager();
        } else if (activity instanceof Fraction) {
            manager = ((Fraction) activity).getFractionAbility().getFractionManager();
        }
        if (manager == null) {
            return null;
        }

        return new PageSliderProvider() {
            @Override
            public int getCount() {
                return fragments.size();
            }

            @Override
            public Object createPageInContainer(ComponentContainer componentContainer, int i) {
                LayoutScatter instance = LayoutScatter.getInstance(componentContainer.getContext());
                Component attached = ((BaseFraction) fragments.get(i)).onComponentAttached(instance, componentContainer, new Intent());
                return attached;
            }

            @Override
            public void destroyPageFromContainer(ComponentContainer componentContainer, int i, Object o) {
                componentContainer.removeComponent(((Component) o));
            }

            @Override
            public boolean isPageMatchToObject(Component component, Object o) {
                return component == o;
            }

            @Override
            public String getPageTitle(int position) {
                if (tittles == null) {
                    return "";
                }
                return position < tittles.size() ? tittles.get(position) : "";
            }
        };
    }

}
